home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / lisp / guile-ii.src / guile-ii / guile-src / slib / logical.scm < prev    next >
Encoding:
Text File  |  1995-01-19  |  4.7 KB  |  151 lines

  1. ;;;; "logical.scm", bit access and operations for integers for Scheme
  2. ;;; Copyright (C) 1991, 1993 Aubrey Jaffer.
  3. ;
  4. ;Permission to copy this software, to redistribute it, and to use it
  5. ;for any purpose is granted, subject to the following restrictions and
  6. ;understandings.
  7. ;
  8. ;1.  Any copy made of this software must include this copyright notice
  9. ;in full.
  10. ;
  11. ;2.  I have made no warrantee or representation that the operation of
  12. ;this software will be error-free, and I am under no obligation to
  13. ;provide any services, by way of maintenance, update, or otherwise.
  14. ;
  15. ;3.  In conjunction with products arising from the use of this
  16. ;material, there shall be no use of my name in any advertising,
  17. ;promotional, or sales literature without prior written consent in
  18. ;each case.
  19.  
  20. (define logical:integer-expt
  21.   (if (provided? 'inexact)
  22.       expt
  23.       (lambda (n k)
  24.     (logical:ipow-by-squaring n k 1 *))))
  25.  
  26. (define (logical:ipow-by-squaring x k acc proc)
  27.   (cond ((zero? k) acc)
  28.     ((= 1 k) (proc acc x))
  29.     (else (logical:ipow-by-squaring (proc x x)
  30.                     (quotient k 2)
  31.                     (if (even? k) acc (proc acc x))
  32.                     proc))))
  33.  
  34. (define (logical:logand n1 n2)
  35.   (cond ((= n1 n2) n1)
  36.     ((zero? n1) 0)
  37.     ((zero? n2) 0)
  38.     (else
  39.      (+ (* (logical:logand (logical:ash-4 n1) (logical:ash-4 n2)) 16)
  40.         (vector-ref (vector-ref logical:boole-and (modulo n1 16))
  41.             (modulo n2 16))))))
  42.  
  43. (define (logical:logior n1 n2)
  44.   (cond ((= n1 n2) n1)
  45.     ((zero? n1) n2)
  46.     ((zero? n2) n1)
  47.     (else
  48.      (+ (* (logical:logior (logical:ash-4 n1) (logical:ash-4 n2)) 16)
  49.         (- 15 (vector-ref (vector-ref logical:boole-and
  50.                       (- 15 (modulo n1 16)))
  51.                   (- 15 (modulo n2 16))))))))
  52.  
  53. (define (logical:logxor n1 n2)
  54.   (cond ((= n1 n2) 0)
  55.     ((zero? n1) n2)
  56.     ((zero? n2) n1)
  57.     (else
  58.      (+ (* (logical:logxor (logical:ash-4 n1) (logical:ash-4 n2)) 16)
  59.         (vector-ref (vector-ref logical:boole-xor (modulo n1 16))
  60.             (modulo n2 16))))))
  61.  
  62. (define (logical:lognot n) (- -1 n))
  63.  
  64. (define (logical:logtest int1 int2)
  65.   (not (zero? (logical:logand int1 int2))))
  66.  
  67. (define (logical:logbit? index int)
  68.   (logical:logtest (logical:integer-expt 2 index) int))
  69.  
  70. (define (logical:bit-extract n start end)
  71.   (logical:logand (- (logical:integer-expt 2 (- end start)) 1)
  72.           (logical:ash n (- start))))
  73.  
  74. (define (logical:ash int cnt)
  75.   (if (negative? cnt)
  76.       (let ((n (logical:integer-expt 2 (- cnt))))
  77.     (if (negative? int)
  78.         (+ -1 (quotient (+ 1 int) n))
  79.         (quotient int n)))
  80.       (* (logical:integer-expt 2 cnt) int)))
  81.  
  82. (define (logical:ash-4 x)
  83.   (if (negative? x)
  84.       (+ -1 (quotient (+ 1 x) 16))
  85.       (quotient x 16)))
  86.  
  87. (define (logical:logcount n)
  88.   (cond ((zero? n) 0)
  89.     ((negative? n) (logical:logcount (logical:lognot n)))
  90.     (else
  91.      (+ (logical:logcount (logical:ash-4 n))
  92.         (vector-ref '#(0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4)
  93.             (modulo n 16))))))
  94.  
  95. (define (logical:integer-length n)
  96.   (case n
  97.     ((0 -1) 0)
  98.     ((1 -2) 1)
  99.     ((2 3 -3 -4) 2)
  100.     ((4 5 6 7 -5 -6 -7 -8) 3)
  101.     (else (+ 4 (logical:integer-length (logical:ash-4 n))))))
  102.  
  103. (define logical:boole-xor
  104.  '#(#(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)
  105.     #(1 0 3 2 5 4 7 6 9 8 11 10 13 12 15 14)
  106.     #(2 3 0 1 6 7 4 5 10 11 8 9 14 15 12 13)
  107.     #(3 2 1 0 7 6 5 4 11 10 9 8 15 14 13 12)
  108.     #(4 5 6 7 0 1 2 3 12 13 14 15 8 9 10 11)
  109.     #(5 4 7 6 1 0 3 2 13 12 15 14 9 8 11 10)
  110.     #(6 7 4 5 2 3 0 1 14 15 12 13 10 11 8 9)
  111.     #(7 6 5 4 3 2 1 0 15 14 13 12 11 10 9 8)
  112.     #(8 9 10 11 12 13 14 15 0 1 2 3 4 5 6 7)
  113.     #(9 8 11 10 13 12 15 14 1 0 3 2 5 4 7 6)
  114.     #(10 11 8 9 14 15 12 13 2 3 0 1 6 7 4 5)
  115.     #(11 10 9 8 15 14 13 12 3 2 1 0 7 6 5 4)
  116.     #(12 13 14 15 8 9 10 11 4 5 6 7 0 1 2 3)
  117.     #(13 12 15 14 9 8 11 10 5 4 7 6 1 0 3 2)
  118.     #(14 15 12 13 10 11 8 9 6 7 4 5 2 3 0 1)
  119.     #(15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0)))
  120.  
  121. (define logical:boole-and
  122.  '#(#(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
  123.     #(0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1)
  124.     #(0 0 2 2 0 0 2 2 0 0 2 2 0 0 2 2)
  125.     #(0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3)
  126.     #(0 0 0 0 4 4 4 4 0 0 0 0 4 4 4 4)
  127.     #(0 1 0 1 4 5 4 5 0 1 0 1 4 5 4 5)
  128.     #(0 0 2 2 4 4 6 6 0 0 2 2 4 4 6 6)
  129.     #(0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7)
  130.     #(0 0 0 0 0 0 0 0 8 8 8 8 8 8 8 8)
  131.     #(0 1 0 1 0 1 0 1 8 9 8 9 8 9 8 9)
  132.     #(0 0 2 2 0 0 2 2 8 8 10 10 8 8 10 10)
  133.     #(0 1 2 3 0 1 2 3 8 9 10 11 8 9 10 11)
  134.     #(0 0 0 0 4 4 4 4 8 8 8 8 12 12 12 12)
  135.     #(0 1 0 1 4 5 4 5 8 9 8 9 12 13 12 13)
  136.     #(0 0 2 2 4 4 6 6 8 8 10 10 12 12 14 14)
  137.     #(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)))
  138.  
  139. (define logand logical:logand)
  140. (define logior logical:logior)
  141. (define logxor logical:logxor)
  142. (define lognot logical:lognot)
  143. (define logtest logical:logtest)
  144. (define logbit? logical:logbit?)
  145. (define ash logical:ash)
  146. (define logcount logical:logcount)
  147. (define integer-length logical:integer-length)
  148. (define bit-extract logical:bit-extract)
  149. (define ipow-by-squaring logical:ipow-by-squaring)
  150. (define integer-expt logical:integer-expt)
  151.